#ifndef __R_TOOLS_H__
#define __R_TOOLS_H__

#include "Stream/ugdefs.h"
#include "Geometry/UGGeometry.h"

namespace RSuperMap
{
	using namespace UGC;
	class RTools
	{
	private:
		RTools() {}
	public:
		static  SEXP wrap_str(const char* pstr)
		{
			SEXP sexp;
			PROTECT(sexp = Rf_allocVector(STRSXP, 1));
			SET_STRING_ELT(sexp, (R_len_t)0, Rf_mkCharCE(pstr, CE_UTF8));
			UNPROTECT(1);
			return sexp;
		}


		static std::string as_str(SEXP sexp)
		{
			if(!sexp)
			{
				return std::string();
			}
			if (TYPEOF(sexp) == CHARSXP)
			{
				return CHAR(sexp);
			}
			if (!Rf_isString(sexp))
			{
				throw ::Rcpp::not_compatible("RTools:: expecting a string");
			}
			if (Rf_length(sexp) < 1)
			{
				return std::string();
			}
			std::string out;
			out.assign(Rf_translateCharUTF8(STRING_ELT(sexp, 0)));
			return out;
		}

		static char* copy_str(SEXP sexp)
		{
			string strValue =  RTools::as_str(sexp);
			return CopyStr(strValue.c_str());
		}
	};


}

#endif
